package com.zb.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.base.OmsMetric;
import com.zb.service.base.MetricService;
import com.zb.util.ActionUtils;

/**
 * 
 * @author huanghao
 *
 */
@Controller
@RequestMapping("/metricApi")
public class MetricAction extends BaseAction
{
	@Autowired
	private MetricService metricServiceImpl;
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
	{
		return metricServiceImpl.add(this.getObjFromRequest(request,true,false));
	}
	
	@RequestMapping(value="update/{metric_type}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> updateOperMerchant(@PathVariable(value="metric_type") String metric_type,
			HttpServletRequest request, HttpServletResponse response)
	{
		OmsMetric metric = this.getObjFromRequest(request,false,false);
		if (StringUtils.isNotEmpty(metric_type))
		{
			metric.setMetric_type(metric_type);
		}
		return metricServiceImpl.update(metric);
	}
	
	@RequestMapping(value="delete/{metric_type}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteOperMerchant(@PathVariable(value="metric_type") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return metricServiceImpl.delete(id);
	}
	
	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllOrPage(HttpServletRequest request, HttpServletResponse response)
	{
		return metricServiceImpl.findPageObjs(this.buildQueryParam(request, true), this.getObjFromRequest(request,false,true));
	}
	
	@RequestMapping(value="findObj/{metric_type}",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findObj(@PathVariable(value="metric_type") String metric_type,
			HttpServletRequest request, HttpServletResponse response)
	{
		return metricServiceImpl.findById(metric_type);
	}
	
	private OmsMetric getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		String loginUserId = this.getLoginUserId(request);
		return ActionUtils.getParamInstance(OmsMetric.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
}
